package com.steve.xfood.controller;

import com.steve.xfood.VO.ResultVO;
import com.steve.xfood.convert.OrderForm2OrderDTO;
import com.steve.xfood.dto.OrderDTO;
import com.steve.xfood.enums.ResultEnum;
import com.steve.xfood.exception.XFoodException;
import com.steve.xfood.form.OrderForm;
import com.steve.xfood.service.BuyerService;
import com.steve.xfood.service.OrderService;
import com.steve.xfood.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 买家订单
 * @author steve
 * @date 2020/5/9 15:25
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     * @param orderForm 前端参数
     * @param bindingResult 参数校验结果
     * @return
     */
    @PostMapping("/create")
    public ResultVO<OrderDTO> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        //参数校验
        if (bindingResult.hasErrors()) {
            //参数校验有误，则打印日志和抛异常
            log.info("【创建订单】 参数不正确 OrderForm={}",orderForm);
            //返回异常码和异常信息
            throw new XFoodException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        //参数转换
        OrderDTO orderDTOConvert = OrderForm2OrderDTO.convert(orderForm);
        //获取数据库数据
        OrderDTO orderDTOCreate = orderService.create(orderDTOConvert);
        /*封装返回给前端的参数
        {
          "code": 0,
          "msg": "成功",
          "data": {
              "orderId": "147283992738221"
          }
        }
         */
        Map<String, String> map = new HashMap<>();
        map.put("orderId", orderDTOCreate.getOrderId());
        return ResultVOUtil.success(map);
    }

    /**
     *
     * 查询订单列表
     * @param openId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public ResultVO<OrderDTO> list(@RequestParam("openid") String openId,
                                   @RequestParam(value = "page",defaultValue = "0") Integer page,
                                   @RequestParam(value = "size",defaultValue = "2") Integer size) {
        if (StringUtils.isEmpty(openId)) {
            log.info("【查询订单列表】openid为空");
            throw new XFoodException(ResultEnum.PARAM_ERROR);
        }
        //PageRequest pageRequest = new PageRequest(page, size); springboot 1.5
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<OrderDTO> orderDTOPage = orderService.findAll(openId, pageRequest);

        //时间显示转存Date -> Long(通过工具类)

        return ResultVOUtil.success(orderDTOPage.getContent());
    }

    /**
     * 查询订单详情
     * @param openId
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openId,
                                     @RequestParam("orderId") String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            log.info("【查询订单详情】orderId为空");
            throw new XFoodException(ResultEnum.PARAM_ERROR);
        }
        OrderDTO orderDTO = buyerService.findOrderOne(openId, orderId);
        return ResultVOUtil.success(orderDTO);
    }

    /**
     * 取消订单
     * @param openId
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openId,
                           @RequestParam("orderId") String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            log.info("【取消订单】orderId为空");
            throw new XFoodException(ResultEnum.PARAM_ERROR);
        }
        buyerService.orderCancel(openId, orderId);
        return ResultVOUtil.success();
    }
}
